//
// Created by Tony on 2024/6/14.
//

#include "Method.h"
#include "../../common/KeyWords.h"
#include "../rt_code/Class.h"
#include "../rt_code/ClassTable.h"
#include <memory>
#include <sstream>

namespace RtCode {

using std::make_shared;

Method::Method(const string& name) : Member(name) { this->mCode = make_shared<Code>(); }

void Method::setType(const vector<int32_t>& parameterTypes, int32_t returnType) {
    this->mReturnType = returnType;
    this->mParameterTypes = parameterTypes;
}

bool Method::isReturnVoid() const {
    shared_ptr<Class> ownerClass = getOwnerClass();
    shared_ptr<ClassTable> classTable = ownerClass->getClassTable();
    return mReturnType == classTable->getClassIdByName(KeyWords::VOID);
}

std::string Method::toString() {
    using std::endl;
    std::stringstream ss;
    ss << "method: " << getName() << endl;
    ss << getAccessFlag().toString() << endl;
    ss << "Code:" << endl;
    ss << "stacks=" << mCode->getMaxStack() << ", locals=" << mCode->getLocalSlotCount()
       << ", args=" << mArgSlotCount << endl;
    ss << mCode->toString();
    return ss.str();
}

} // namespace RtCode